Fix ordertab() segfault when nunique is incorrectly supplied - #207
Merged
Conversation
The ordertab() C function was susceptible to a buffer overflow when the 'nunique' parameter passed from R was smaller than the actual number of unique values in the data. This led to intermittent segfaults due to out-of-bounds writes to the 'ret' array. This commit introduces a bounds check in the C implementation of ordertab() (r_ram_integer64_ordertab_asc). If the number of unique values encountered exceeds the allocated size of the 'ret' vector, the loop is now terminated early, preventing any out-of-bounds writes and thus eliminating the segfault. The function will now return a truncated result in such cases, consistent with R's usual handling of mismatched lengths. A new test case has been added to test-sortuse64.R to specifically target this scenario, using gctorture() to reliably trigger the condition. This test now passes, confirming the fix. Updated NEWS.md to reflect this bug fix.
hcirellu
pushed a commit
to hcirellu/bit64
that referenced
this pull request
Jan 3, 2026
* Fix: ordertab() segfaults when nunique is smaller than actual The ordertab() C function was susceptible to a buffer overflow when the 'nunique' parameter passed from R was smaller than the actual number of unique values in the data. This led to intermittent segfaults due to out-of-bounds writes to the 'ret' array. This commit introduces a bounds check in the C implementation of ordertab() (r_ram_integer64_ordertab_asc). If the number of unique values encountered exceeds the allocated size of the 'ret' vector, the loop is now terminated early, preventing any out-of-bounds writes and thus eliminating the segfault. The function will now return a truncated result in such cases, consistent with R's usual handling of mismatched lengths. A new test case has been added to test-sortuse64.R to specifically target this scenario, using gctorture() to reliably trigger the condition. This test now passes, confirming the fix. Updated NEWS.md to reflect this bug fix. * To taste * tighten test * comment purpose * citation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #168. Co-written with Gemini.